#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <numeric>

using namespace std;

int minOperations(vector<int>& nums, int x) {
	int n = nums.size();
	int sum = accumulate(nums.begin(), nums.end(), 0);
	if (sum < x) return -1;
	int let = 0, ret = 0, l = -1, r = 0, len = n + 1;
	while (l < n) {
		if (l != -1) let += nums[l];

		while (r < n && ret + let > x) {
			ret -= nums[r];
			r++;
		}

		if (let + ret == x)
			len = min(len, (l + 1) + (n - r));

		l++;
	}
	if (len > n)
		return -1;
	else
		return len;
}

int main() {
	vector<int> v = { 1,1,4,2,3 };
	minOperations(v, 5);
}